﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NXT2Adapter;
using System.IO.Pipes;
using System.Diagnostics;
using System.Threading;
using Xnaml;
using Simulation;
using System.Windows.Controls;
using System.Windows;

namespace NXT2Adapter
{
    public class SimulationControl : RobotControl
    {
        private Kinixt.SimulationGame robotSimulation;
        private Canvas childCanvas;

        /// <summary>
        /// Initiates control of simulation
        /// </summary>
        /// <param name="canvas">Canvas on which simulation will be displayed</param>
        public SimulationControl(Canvas canvas)
        {
            childCanvas = canvas;

            robotSimulation = new Kinixt.SimulationGame();
            robotSimulation.Name = "Simulation";
            robotSimulation.Width = System.Windows.SystemParameters.PrimaryScreenWidth - Canvas.GetLeft(childCanvas);
            robotSimulation.Height = System.Windows.SystemParameters.PrimaryScreenHeight - Canvas.GetTop(childCanvas);

            childCanvas.Children.Add(robotSimulation);
        }

        public Kinixt.SimulationGame RobotSimulation
        {
            get { return robotSimulation; }
            private set { robotSimulation = value; }
        }

        /// <summary>
        /// Send fork lift command to robot
        /// </summary>
        /// <param name="position">New poition of fork</param>
        public override void ControlFork(double position)
        {
            CommunicationProtocol communicat = new CommunicationProtocol();
            communicat.Command = "controlFork";
            communicat.ValueFloat = (float)position;
            robotSimulation.SendMessage(this, communicat);
        }

        /// <summary>
        /// Send move command to robot
        /// </summary>
        /// <param name="power">Power given to the engines</param>
        /// <param name="tachoLimit">Distance of movement. In Degrees </param>
        /// <param name="turnRatio">Turn ratio</param>
        public override void Move(double power, double tachoLimit, double turnRatio)
        {
            CommunicationProtocol communicatPower = new CommunicationProtocol();
            communicatPower.Command = "power";
            communicatPower.ValueFloat = (float)power;
            robotSimulation.SendMessage(this, communicatPower);

            // not used in simulation
            //CommunicationProtocol communicatTacho = new CommunicationProtocol();
            //communicatTacho.Command = "tachoLimit";
            //communicatTacho.ValueFloat = (float)tachoLimit;
            //robotSimulation.SendMessage(this, communicatTacho);

            CommunicationProtocol communicatTurn = new CommunicationProtocol();
            communicatTurn.Command = "turnRatio";
            communicatTurn.ValueFloat = (float)turnRatio;
            robotSimulation.SendMessage(this, communicatTurn);
        }

        /// <summary>
        /// Connect robot
        /// </summary>
        /// <returns></returns>
        public override bool Connect()
        {
            robotSimulation.SendMessage(this, "connect");
            return true;
        }

        /// <summary>
        /// Disconect robot
        /// </summary>
        public override void Disconnect()
        {
            robotSimulation.SendMessage(this, "exit");
        }

        /// <summary>
        /// Idle robot
        /// </summary>
        public override void Idle()
        {
            robotSimulation.SendMessage(this, "idle");
        }

        /// <summary>
        /// Idel robot fork
        /// </summary>
        public override void IdleFork()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Idle robot movement
        /// </summary>
        public override void IdleMove()
        {
            throw new NotImplementedException();
        }

    }
}
